# 作者： 迷路的小怪兽
# 创建时间： 2021/10/20 21:40
from selenium import webdriver
import os
from src.common.getlog import MyLog


class MyDriver:
    """
    初始化浏览器驱动，打开浏览器
    """

    def __init__(self, mode='normal', browser='chrome', driver_path=None, wait_time=10):
        """
        设置浏览器模式与默认等待时间
        :param mode: 浏览器模式，‘headless’为无界面模式
        :param wait_time: 元素隐式等待时间
        """
        if driver_path is not None:
            self.driver_path = driver_path
        else:
            self.driver_path = f"{os.path.abspath('.').split('auto-test-project')[0]}auto-test-project\\lib"
        self.mode = mode
        self.wait_time = wait_time
        self.log = MyLog()
        self.browser = browser.lower()

    def open_browser(self):
        if self.browser == 'chrome':
            browser_driver_name = 'chromedriver.exe'
            driver = self._chrome_browser(browser_driver_name)
        elif self.browser == 'firefox':
            browser_driver_name = 'geckodriver.exe'
            driver = self._firefox_browser(browser_driver_name)
        elif self.browser == 'edge':
            browser_driver_name = 'edgedriver.exe'
            driver = self._edge_browser(browser_driver_name)
        elif self.browser == 'ie':
            browser_driver_name = 'iedriverserver.exe'
            driver = self._ie_browser(browser_driver_name)
        else:
            browser_driver_name = 'chromedriver.exe'
            driver = self._chrome_browser(browser_driver_name)
            self.log.warning(f'浏览器驱动名称出错！请检查{self.browser}')
            self.log.info(f'为保证测试顺利进行，已默认为您打开谷歌浏览器！')

        driver.implicitly_wait(self.wait_time)
        driver.maximize_window()
        self.log.info(f'浏览器已打开！当前浏览器模式为：{self.mode}')

        return driver

    def _chrome_browser(self, browser_driver_name):
        """
        打开Chrome浏览器
        :param driver_path: 浏览器驱动路径
        :return: 浏览器对象
        """
        exe_path = f"{self.driver_path}\\{browser_driver_name}"

        if self.mode.lower() == 'headless':
            option = webdriver.ChromeOptions()
            option.add_argument('--headless')
        # TODO: seleniumserver 节点分发执行模式暂未更新
        driver = webdriver.Chrome(executable_path=exe_path)
        return driver

    def _firefox_browser(self, browser_driver_name):
        """
        打开FireFox浏览器
        :param driver_path: 浏览器驱动路径
        :return: 浏览器对象
        """
        exe_path = f"{self.driver_path}\\{browser_driver_name}"

        if self.mode.lower() == 'headless':
            option = webdriver.FirefoxOptions()
            option.add_argument('--headless')

        driver = webdriver.Firefox(executable_path=exe_path)
        return driver

    def _edge_browser(self, browser_driver_name):
        """
        打开Edge浏览器
        :param driver_path: 浏览器驱动路径
        :return: 浏览器对象
        """
        exe_path = f"{self.driver_path}\\{browser_driver_name}"
        self.log.info('edge浏览器不支持无界面模式！')
        driver = webdriver.Edge(executable_path=exe_path)
        return driver

    def _ie_browser(self, browser_driver_name):
        """
        打开IE浏览器
        :param driver_path: 浏览器驱动路径
        :return: 浏览器对象
        """
        exe_path = f"{self.driver_path}\\{browser_driver_name}"

        if self.mode.lower() == 'headless':
            option = webdriver.IeOptions()
            option.add_argument('--headless')

        driver = webdriver.Ie(executable_path=exe_path)
        return driver
